package br.usp.ime.dojo.core.repositories.impl.memoria;

import java.util.ArrayList;

import br.usp.ime.dojo.core.entities.DojoRoom;
import br.usp.ime.dojo.core.exceptions.DuplicatedRoomException;
import br.usp.ime.dojo.core.repositories.DojoRoomRepository;

public class DojoRoomRepositoryInMemory implements DojoRoomRepository {

	private ArrayList<DojoRoom> dojoRoomList;
	private static DojoRoomRepositoryInMemory instance = null;


	public static DojoRoomRepositoryInMemory getInstance() {
		if (instance == null) {
			instance = new DojoRoomRepositoryInMemory();
		}
		return instance;
	}

	private DojoRoomRepositoryInMemory() {
		dojoRoomList = new ArrayList<DojoRoom>();
	}

	public ArrayList<DojoRoom> getDojoRoomList() {
		return dojoRoomList;
	}

	public void addRoom(DojoRoom dojoRoom) throws DuplicatedRoomException {
		if (get(dojoRoom.getName()) != null)
			throw new DuplicatedRoomException();
		dojoRoomList.add(dojoRoom);
	}

	public DojoRoom get(String roomName) {
		for (DojoRoom room : dojoRoomList) {
			if (room.getName().equals(roomName)) {
				return room;
			}
		}
		return null;
	}

	public int count() {
		return dojoRoomList.size();
	}

	public void clearAll() {
		dojoRoomList.clear();
	}

}
